package com.szholly.pro.watergas_server.ps.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.szholly.pro.watergas_server.common.data.DataUtil;
import com.szholly.pro.watergas_server.common.web.BaseController;
import com.szholly.pro.watergas_server.common.web.RequestUnit;
import com.szholly.pro.watergas_server.ps.service.StationCheckinfoService;
import com.szholly.utils.spring.SpringBeanFactory;
import com.szholly.data.general.Field;
import com.szholly.data.general.ITable;
import com.szholly.data.general.ITableFactory;
import com.szholly.data.general.PageInfo;
import com.szholly.data.general.RowBase;
import com.szholly.plug.form.excel.utils.StringUtil;

import net.sf.json.JSONObject;

@Controller
@RequestMapping(value="/StationCheckInfo/*")
public class StationCheckInfoController extends BaseController{
	
	protected Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	StationCheckinfoService stationCheckinfoService;
	
	/**
	 * 现场考核信息查询
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value="/queryStationCheckInofList")
	public void queryStationCheckInfo(HttpServletRequest resq, HttpServletResponse res) throws Exception{
		String station_id=RequestUnit.getString("STATION_ID");
		String start=RequestUnit.getString("startDate");
		String end=RequestUnit.getString("endDate");
		String status=RequestUnit.getString("CHECK_STATUS_DESC");
		Map<String,String> result=new HashMap<>();
		if(StringUtil.IsNotNullOrEmpty(station_id)){
			result.put("station_id", station_id);
		}	
		if(StringUtil.IsNotNullOrEmpty(start)){
			result.put("start",start);
		}
		if(StringUtil.IsNotNullOrEmpty(end)){
			result.put("end",end);
		}
		if(StringUtil.IsNotNullOrEmpty(status)){
			result.put("status",status);
		}
		ITableFactory tableFactory = (ITableFactory) SpringBeanFactory.getBean("TableFactory");
		ITable pTable = tableFactory.openTable("SUPPLY_WATER_STATION_CHECK");		
		PageInfo pageInfo = DataUtil.getPageInfo();
		List<RowBase> list =stationCheckinfoService.queryStationCheckList(result, pTable, pageInfo);//现场考核
		JSONObject JSON=DataUtil.listToJSONObject(list, null, pageInfo);
		outString(JSON.toString());
	}
	/**
	 * 现场考核项详情信息信息获取
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value="/queryStationCheckItemInfoList")
	public void queryStationCheckItemInfoList(HttpServletRequest resq, HttpServletResponse res) throws Exception{
		Map<String,String> result=new HashMap<>();
		String checkid=RequestUnit.getString("GUID");
		if(StringUtil.IsNotNullOrEmpty(checkid)){
			result.put("checkid", checkid);
		}
		List<RowBase> list=stationCheckinfoService.queryStationCheckItemList(result, null);
		Map<String,Object> rowresult=new HashMap<>();
		rowresult.put("data", list);
		JSONObject json=DataUtil.listToJSONObject(list);
		if(list.size()>0){
			json.put("itemcount", list.size()+1);
		}else{
			json.put("itemcount", "0");
		}
		
		outString(json.toString());
	}
	/**
	 * 获取现场检测人员名单
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value="/queryOperationPersonList")
	public void queryOperationPersonList(HttpServletRequest resq, HttpServletResponse res) throws Exception{
		Map<String,String> result=new HashMap<>();
		List<RowBase> list=stationCheckinfoService.queryOperationPersonList(result, null);
		Map<String,Object> rowresult=new HashMap<>();
		rowresult.put("data", list);
		JSONObject json=DataUtil.listToJSONObject(list);
		outString(json.toString());
	}
	/**
	 * 获取运营人员名单
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value="/queryOperationNmaeList")
	public void queryOperationNmaeList(HttpServletRequest resq, HttpServletResponse res) throws Exception{
		Map<String,String> result=new HashMap<>();
		List<RowBase> list=stationCheckinfoService.queryOperationNmaeList(result, null);
		Map<String,Object> rowresult=new HashMap<>();
		rowresult.put("data", list);
		JSONObject json=DataUtil.listToJSONObject(list);
		outString(json.toString());
	}
	/**
	 * 现场考核信息编辑
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value="/updateStationCheckInfo")
	public void updateStationCheckInfo(HttpServletRequest resq, HttpServletResponse res)throws Exception{
		HashMap<String,Object> params=RequestUnit.getHashMapParmO();
		Map<String,String> result=new HashMap<>();
		ITableFactory tableFactory = (ITableFactory) SpringBeanFactory.getBean("TableFactory");
		ITable pTable = tableFactory.openTable("SUPPLY_WATER_STATION_CHECK");//获取现场考核表结构
		ITable pTable1 = tableFactory.openTable("SUPPLY_WATER_STATION_CHECKITEM");//获取现场考核从表结构
		Field[] fields = pTable.getFields();
		Field[] fields1=pTable1.getFields();
		String itemguid= (String) params.get("checkData[GUID]");		
		if(StringUtil.IsNotNullOrEmpty(itemguid)){
			result.put("checkid", itemguid);
		}
		List<RowBase> list=stationCheckinfoService.queryStationCheckItemList(result, null);
		HashMap<String, Object> checkData = new HashMap<>();
		for(Field field:fields){//现场考核表，进行循环赋值
			String fieldName = field.getFieldName();
			Object valueObj = params.get("checkData["+fieldName+"]");//赋值
			if(valueObj != null){
				String value = String.valueOf(valueObj);
				checkData.put(fieldName, value);
			}
		}
		HashMap<String, Object> ItemData = new HashMap<>();
		for(int i=0;i<list.size();i++){
		for(Field fiel:fields1){
			String fielName=fiel.getFieldName();			
				Object valueObj = params.get("itemData["+i+"]["+fielName+"]");//赋值
				if(valueObj != null){
					if(valueObj=="EQUIPMENT_VALUE" || valueObj=="STANDARD_VALUE"){
						Number value=(Number) valueObj;
						ItemData.put(fielName, value);
					}else{
						String value= String.valueOf(valueObj);
						ItemData.put(fielName, value);
					}
					
				}
			}
		stationCheckinfoService.updateStationCheckItem(pTable1, ItemData);//现场考核从表
		}
		stationCheckinfoService.updateStationCheck(checkData,pTable);//现场考核
	}
	/**
	 * 现场考核申请编辑以及审核
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value="CheckStationInfo")
	public void CheckStationInfo(HttpServletRequest resq, HttpServletResponse res)throws Exception{
		HashMap<String,Object> params=RequestUnit.getHashMapParmO();
		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
	    Date date = new Date();
	    params.put("CHECK_TIME", f.format(date)); 
		ITableFactory tableFactory = (ITableFactory) SpringBeanFactory.getBean("TableFactory");
		ITable pTable = tableFactory.openTable("SUPPLY_WATER_STATION_CHECK");//获取现场考核表结构		
		stationCheckinfoService.updateStationCheck(params,pTable);//现场考核
	}
	/**
	 * 导出PDF
	 * @param resq
	 * @param res
	 * @throws IOException
	 */
	@RequestMapping(value="StationCheckeportPDF")
	public void StationCheckeexportPDF(HttpServletRequest resq, HttpServletResponse res) throws IOException{
		try {
			String start=RequestUnit.getString("start");
			String end=RequestUnit.getString("end");
			String station_id=RequestUnit.getString("STATION_ID");
			HashMap<String, Object> data = new HashMap<>();
			Map<String,String> result=new HashMap<>();
			if(StringUtil.IsNotNullOrEmpty(start)){
				result.put("start",start);
			}
			if(StringUtil.IsNotNullOrEmpty(end)){
				result.put("end",end);
			}
			if(StringUtil.IsNotNullOrEmpty(station_id)){
				result.put("station_id",station_id);
			}
			List<RowBase> list =stationCheckinfoService.queryStationCheckList(result, null, null);//现场考核
			List<RowBase> list1 =stationCheckinfoService.getStationNum(result);
			List<RowBase> list2 =stationCheckinfoService.getTotalStation(result);
			data.put("checklist", list);
			data.put("stationnum", list1);
			data.put("totalstation", list2);
			data.put("end", end);
			data.put("start", start);
			outPDF(data, "stationCheck.html", "现场信息.pdf",false);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	/**
	 * 考核详情信息导出PDF
	 * @param resq
	 * @param res
	 * @throws IOException
	 */
	@RequestMapping(value="StationCheckePDF")
	public void StationCheckePDF(HttpServletRequest resq, HttpServletResponse res) throws IOException{
		try {
			Map<String,String> result=new HashMap<>();
			String guid=RequestUnit.getString("GUID");
			String station_code=RequestUnit.getString("STATION_CODE");
			String name=RequestUnit.getString("NAME");
			String hostBase=RequestUnit.getString("hostBase");
			String start=RequestUnit.getString("start");
			String end=RequestUnit.getString("end");
			String wdpjsRootPath=RequestUnit.getString("wdpjsRootPath");
			String title="";
			if(StringUtil.IsNotNullOrEmpty(guid)){
				result.put("checkid", guid);
				title=station_code+name;
			}else{
				title="现场考核详情信息";
			}
			if(StringUtil.IsNotNullOrEmpty(start)){
				result.put("start", start);
			}
			if(StringUtil.IsNotNullOrEmpty(end)){
				result.put("end", end);
			}
			//考核详情
			List<RowBase> CheckItemlist=stationCheckinfoService.queryStationCheckItemList(result, null);
			//现场考核
			List<RowBase> Checkinfo =stationCheckinfoService.queryStationCheckList(result, null, null);
			HashMap<String, Object> data = new HashMap<>();
			data.put("CheckItemlist", CheckItemlist);
			data.put("Checkinfo", Checkinfo);
			data.put("hostBase", hostBase);
			data.put("wdpjsRootPath", wdpjsRootPath);
			outPDF(data, "ToStationCheck.html", title+".pdf",true);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}	
}
